<?php

namespace app\api\controller\v1\staff;

use app\model\basic\Parameter;
use app\model\plan\course\StudyCourse;
use app\model\plan\exam\StaffExam;
use app\model\plan\PlanStaff;
use app\Request;
use think\facade\Validate;

class StaffController
{
    public function info(Request $request)
    {
        $staff = $request->staffInfo() ;
        $staffId = $request->staffId() ;

        $total = PlanStaff::where('staff_id' , $staffId)->where('status' , 1)->count() ;

        $total2 = StudyCourse::where('staff_id' , $staffId)
            ->where('status' , 1)
            ->where('study_status' , 9)
            ->sum('courseware_hour') ;
        $total2 = $total2 ? $total2 : 0 ;

        $total3 = StudyCourse::where('staff_id' , $staffId)
            ->where('status' , 1)->count() ;

        $total4 = StaffExam::where('staff_id' , $staffId)
            ->where('exam_progress' , 9)->avg('point') ;

        return app('json')->success([
            'user' => [
                'name' => $staff['username'] ,
                'img_url' => $staff['img_url'] ,
                'mobile' => $staff['mobile'] ,
                'idcard' => $staff['idcard'] ,
                'department_name' => $staff->department->department_name ,
            ] ,
            'stat' => [
                'stat1' => $total ,
                'stat2' => $total2 ,
                'stat3' => $total3 ,
                'stat4' => $total4 ,
            ]
        ]);
    }
    public function info2(Request $request)
    {
        $staff = $request->staffInfo() ;
        return app('json')->success([
            'user' => [
                'name' => $staff['username'] ,
                'img_url' => $staff['img_url'] ,
                'mobile' => $staff['mobile'] ,
                'idcard' => $staff['idcard'] ,
                'department_name' => $staff->department->department_name ,
                'edu' => Parameter::getOneName($staff['edu_id'] , 'para_name') ,
                'job' => Parameter::getOneName($staff['job_id'] , 'para_name') ,
                'start_work_time' => $staff['start_work_time'] > 0 ? date('Y-m-d' , $staff['start_work_time']) : '',
                'entry_time' => $staff['entry_time'] > 0 ? date('Y-m-d' , $staff['entry_time']) : '',
            ] ,

        ]);
    }
    public function pass(Request $request)
    {
        $staff = $request->staffInfo();
        list($staffPass, $pass, $pass2) = $request->getMore([
            ['staff_pass', ''],
            ['pass', ''],
            ['pass2', ''],
        ], true);
        if ($staffPass == '')
        {
            return app('json')->fail('请输入旧密码') ;
        }
        if ($pass == '' )
        {
            return app('json')->fail('请输入新密码') ;
        }
        if(Validate::length($pass,  '6,18') == false){
            return app('json')->fail('请输入6到18位新密码') ;

        }

        if ($pass2 !== $pass)
        {
            return app('json')->fail('2次密码不一致') ;
        }
        if(md5($staffPass) != $staff['password']) {
            return app('json')->fail('旧密码不正确') ;
        }
        $staff->password = md5($pass) ;
        $staff->save() ;
        return app('json')->success('修改成功') ;

    }
    public function page(Request $request)
    {
        list($staffPass, $pass, $pass2) = $request->getMore([
            ['staff_pass', ''],
            ['pass', ''],
            ['pass2', ''],
        ], true);
    }
    public function edit()
    {

    }
}